package com.cogent.dynamicform.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cogent.business.helper.DynamicFormHelper;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.bus.DynamicFormBussiness;
import com.cogent.dynamicform.bus.impl.DynamicFormBussinessImpl;
import com.cogent.dynamicform.cache.DynamicFormCache;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.facade.IDynamicFormFacade;
import com.cogent.dynamicform.form.IDynamicForm;
import com.cogent.dynamicform.form.impl.DateDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DateTimeDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DefQuerySelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DisableDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DistrictLeveSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DistrictSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DumpBtnDynamicFormImpl;
import com.cogent.dynamicform.form.impl.DynTableFormImpl;
import com.cogent.dynamicform.form.impl.FileUploadDynamicFormImpl;
import com.cogent.dynamicform.form.impl.HeadShipSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.HiddenDynamicFormImpl;
import com.cogent.dynamicform.form.impl.ImageFileDynamicFormImpl;
import com.cogent.dynamicform.form.impl.ImgUploadDynamicFormImpl;
import com.cogent.dynamicform.form.impl.LevelSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.MutiImgUploadDynamicFormImpl;
import com.cogent.dynamicform.form.impl.MutiQuerySelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.MutiSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.QueryDynTableFormImpl;
import com.cogent.dynamicform.form.impl.QueryHiddenDynamicFormImpl;
import com.cogent.dynamicform.form.impl.QuerySelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.QuerySelectTextDynamicFormImpl;
import com.cogent.dynamicform.form.impl.RadioDynamicFormImpl;
import com.cogent.dynamicform.form.impl.RichTextDynamicFormImpl;
import com.cogent.dynamicform.form.impl.SelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.SingleMutiSelectDynamicFormImpl;
import com.cogent.dynamicform.form.impl.TextAreaDynamicFormImpl;
import com.cogent.dynamicform.form.impl.TextDynamicFormImpl;

public class DynamicFormFacadeImpl implements IDynamicFormFacade {
	private static Map _FORM_IMPL_MAP = new HashMap();
	static {
		_FORM_IMPL_MAP.put("TEXT", new TextDynamicFormImpl());
		_FORM_IMPL_MAP.put("ENUM", new SelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("DATE", new DateDynamicFormImpl());
		_FORM_IMPL_MAP.put("RADIO", new RadioDynamicFormImpl());
		_FORM_IMPL_MAP.put("HIDDEN", new HiddenDynamicFormImpl());
		_FORM_IMPL_MAP.put("TEXTAREA", new TextAreaDynamicFormImpl());
		_FORM_IMPL_MAP.put("LEVELSELECT", new LevelSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("DISTRICTSELECT", new DistrictSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("RICHTEXT", new RichTextDynamicFormImpl());
		_FORM_IMPL_MAP.put("IMGFILE", new ImageFileDynamicFormImpl());
		_FORM_IMPL_MAP.put("DISTLEVELENUM", new DistrictLeveSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("MUTIENUM", new MutiSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("SINGLEENUM", new SingleMutiSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("QUERYSELECT", new QuerySelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("DEFQUERYSELECT", new DefQuerySelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("IMGUPLOAD", new ImgUploadDynamicFormImpl());
		_FORM_IMPL_MAP.put("MUTIQUERYSELECT", new MutiQuerySelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("QUERYSELECTTEXT", new QuerySelectTextDynamicFormImpl());
		_FORM_IMPL_MAP.put("MUTIIMGUPLOAD", new MutiImgUploadDynamicFormImpl());
		_FORM_IMPL_MAP.put("DUMPBTN", new DumpBtnDynamicFormImpl());
		_FORM_IMPL_MAP.put("QUERYHIDDEN", new QueryHiddenDynamicFormImpl());
		_FORM_IMPL_MAP.put("DISABLED", new DisableDynamicFormImpl());
		_FORM_IMPL_MAP.put("HEADSHIPSELECT", new HeadShipSelectDynamicFormImpl());
		_FORM_IMPL_MAP.put("DATETIME", new DateTimeDynamicFormImpl());
		_FORM_IMPL_MAP.put("SINGLEFILEUPLOAD", new FileUploadDynamicFormImpl());
	}
	
	public FormDomain getForm(String formid) {
		return DynamicFormCache.get(formid);
	}
	
	public void getAddValue(FormDomain formDomain, HashMap formMap) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		dynamicFormBussiness.getAddValue(formDomain, formMap);
	}
	
	public List getQueryList(FormDomain formDomain, HashMap formMap) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		return dynamicFormBussiness.getQueryList(formDomain, formMap);
	}
	
	public String getScript(FormDomain formDomain) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		return dynamicFormBussiness.getScript(formDomain);
	}
	
	public String validator(FormDomain formDomain, HashMap dataMap) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		return dynamicFormBussiness.validator(formDomain, dataMap);
	}
	
	public String dealBussiness(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		return dynamicFormBussiness.dealBussiness(bussinessFacade, formDomain, dataMap, true);
	}
	
	public String dealBussiness(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, boolean ispda) {
		DynamicFormBussiness dynamicFormBussiness = new DynamicFormBussinessImpl();
		return dynamicFormBussiness.dealBussiness(bussinessFacade, formDomain, dataMap,ispda);
	}
	
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		IDynamicForm dynamicForm = (IDynamicForm)_FORM_IMPL_MAP.get(formColumnDomain.getType());
		return dynamicForm.genQueryField(formColumnDomain, formMap);
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		IDynamicForm dynamicForm = (IDynamicForm)_FORM_IMPL_MAP.get(formColumnDomain.getType());
		return dynamicForm.genInputField(formColumnDomain, formMap);
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		IDynamicForm dynamicForm = (IDynamicForm)_FORM_IMPL_MAP.get(formColumnDomain.getType());
		return dynamicForm.getShowValue(formColumnDomain, formMap);
	}
	
	public String genDynTableInput(FormDomain formdomain, List formColumnDomainList, HashMap formMap) {
		DynTableFormImpl dynTableForm = new DynTableFormImpl();
		return dynTableForm.genDynTableInput(formdomain, formColumnDomainList, formMap);
	}
	
	public String genDynTableView(FormDomain formdomain, List formColumnDomainList, HashMap datamap) {
		DynTableFormImpl dynTableForm = new DynTableFormImpl();
		return dynTableForm.genDynTableView(formdomain, formColumnDomainList, datamap);
	}
	
	public String getDynTableView(List formColumnDomainList, List viewVlaueList) {
		DynTableFormImpl dynTableForm = new DynTableFormImpl();
		return dynTableForm.getDynTableView(formColumnDomainList, viewVlaueList);
	}
	
	public void dealFormValue(FormDomain formDomain, HashMap formMap) {
		Iterator it = formDomain.getColumnList().iterator();
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getViewmodel() > 0)
				continue;
			dealFormValue(formColumnDomain, formMap);
		}
	}
	
	public void dealFormValue(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return;
		if(_FORM_IMPL_MAP.containsKey(formColumnDomain.getType())) {
				IDynamicForm dynamicForm = (IDynamicForm)_FORM_IMPL_MAP.get(formColumnDomain.getType());
				dynamicForm.dealFormValue(formColumnDomain, formMap);
		}
	}	
	
	public String genQueryDynTable(FormDomain formDomain, List formColumnDomainList, HashMap formMap) {
		QueryDynTableFormImpl queryDynTableForm = new QueryDynTableFormImpl();
		return queryDynTableForm.genQueryDynTableInput(formDomain, formColumnDomainList, formMap);
	}
	
	public String genQueryDynTableView(FormDomain formDomain, HashMap datamap) {
		QueryDynTableFormImpl queryDynTableForm = new QueryDynTableFormImpl();
		Object obj = datamap.get(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY);
		List list = new ArrayList();
		if(obj != null)
			list = (List)obj;
		return queryDynTableForm.getQueryDynTableView(formDomain.getQueryDynTableDomain().getDynTableColumnList(), list);
	}
}
